package edu.nctu.csie.jichang.dp.behavioral;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Iterator {
	public static void main(String[] args) {
		list(new IteratorAry(new String[]{"1","2","3","4"}));
		list(new IteratorList(Arrays.asList(new String[]{"1","2","3","4"})));
	}
	private static void list(java.util.Iterator<String> tIterator) {
		while (tIterator.hasNext()) {
			System.out.println(tIterator.next());
		}
	}
}
class IteratorAry implements java.util.Iterator<String>{
	String[] content;
	IteratorAry(String[] content) { this.content = content;}
	int tIndex = 0;
	public boolean hasNext() {
		return tIndex < content.length;
	}
	public String next() {
		return content[tIndex++];
	}
	public void remove() {
		List<String> tNew = new ArrayList<String>();
		for (int i = 0; i < content.length; i++) {
			if(i != tIndex){
				tNew.add(content[i]);
			}
		}
		content = tNew.toArray(new String[0]);
	}
}
class IteratorList implements java.util.Iterator<String>{
	List<String> content;
	IteratorList(List<String>  content) { this.content = content;}
	int tIndex = 0;
	public boolean hasNext() {
		return tIndex<content.size();
	}
	public String next() {
		return content.get(tIndex++);
	}
	public void remove() {
		content.remove(tIndex);
	}
}